package org.yzh.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yzh.protocol.basics.JTMessage;
import org.yzh.protocol.t808.T0100;
import org.yzh.protocol.t808.T0102;
import org.yzh.protocol.t808.T0200;
import org.yzh.web.mapper.DeviceMapper;
import org.yzh.web.model.util.TokenUtil;
import org.yzh.web.service.IDeviceService;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Transactional
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private TokenUtil tokenUtil;
    @Override
    public int insertBeat(JTMessage jtMessage) {
        return deviceMapper.insertBeatT1000(jtMessage);
    }

    @Override
    public int insertLocation(List<T0200> list) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        list.forEach(x->{
            count.set(deviceMapper.insertLocationT0200(x));
        });
        return count.get();
    }

    @Override
    public String register(T0100 t0100) {
        T0100 t01001 = deviceMapper.selectT0100ByClientId(t0100.getClientId());
        if (t01001 == null) {
            deviceMapper.insertRegisterT0100(t0100);
        }
        return t0100.getDeviceId()+","+t0100.getPlateNo();
    }

    @Override
    public void authentication(T0102 t0102) {
        T0102 t01002 = deviceMapper.selectT0102ByClientId(t0102.getClientId());
        if (t01002 == null) {
            deviceMapper.insertAuthT0102(t0102);
        }
//        return t0102.getClientId()+","+t0102.getToken();
    }
}
